import { colorHex } from "../utils/index";
export default function drawImage(img) {
    const { width, height } = this;
    this.addHistoryColors();
    let canvas = document.createElement("canvas");
    let cxt = canvas.getContext("2d");
    let pxList = [];
    canvas.width = cxt.width = this.width;
    canvas.height = cxt.height = this.height;
    let w = img.width,
      h = img.height;
    let v = w / h;
    if (v > 1) {
      w = width;
      h = w / v;
    } else {
      h = height;
      w = h * v;
    }
    cxt.drawImage(img, (width - w) / 2, (height - h) / 2, w, h);
    for (let i = 0; i < width; i++) {
      for (let j = 0; j < height; j++) {
        let pixel = cxt.getImageData(i, j, 1, 1).data;
        let color = "";
        if (pixel[3] != 0) {
          color = colorHex(`rgb(${pixel[0]},${pixel[1]},${pixel[2]}`).replace(
            "#",
            "0x"
          );
        }
        pxList.push({ color });
      }
    }
    this.render(pxList);
};
